﻿
Public Class nuevo_usuario
    Dim id As String

    Dim flag As Boolean = False
    Dim dataset As New DataSet
    Public Sub setid(ByVal id As Integer)
        Me.id = id
        flag = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim qry As String
        Dim idg As String
        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "insert into usuarios (usr_rut, usr_nombre, usr_apellido, grup_id,usr_clave,usr_email,usr_telefono,usr_tele_fijo) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & ComboBox1.SelectedValue & "','" & encriptar(TextBox4.Text) & "','" & TextBox7.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "')"
            qry = qry & " ;SELECT @@Identity AS Indentity"
            'TextBox1.Text = qry
            Consulta(qry, dataset)
            idg = dataset.Tables("data").Rows(0)(0)
            For Each control In Me.GroupBox1.Controls
                If TypeOf control Is CheckBox Then
                    EjecutaConsulta("insert into privilegios values('" & idg & "','" & control.Name & "','" & control.CheckState & "')")
                End If
            Next
            Me.Close()
            Try

            Catch ex As SqlClient.SqlException
                MsgBox("Error: " & ex.Message)
            End Try
            'registro movimiento
            registro("Agregar", login.usuario.id, main.modulo.usuario)
        End If
        man_usuarios.actualizar()
    End Sub

    Private Sub nuevo_usuario_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Icon = (My.Resources.favicon)
        CargarCombobox("select * from grupos", ComboBox1, "grup_id", "grup_nombre")
        If (flag) Then
            Me.Text = "Editar Usuario"
            Label5.Text = "Editar Usuario"
            Button1.Hide()
            Button3.Show()
            'carga
            Consulta("select * from usuarios where usr_id='" & id & "'", dataset)
            TextBox1.Text = dataset.Tables("data").Rows(0)(1)
            TextBox2.Text = dataset.Tables("data").Rows(0)(2)
            TextBox3.Text = dataset.Tables("data").Rows(0)(3)
            TextBox5.Text = dataset.Tables("data").Rows(0)(7)
            TextBox6.Text = dataset.Tables("data").Rows(0)(8)
            TextBox7.Text = dataset.Tables("data").Rows(0)(6)
            ComboBox1.SelectedValue = dataset.Tables("data").Rows(0)(4)

            Consulta("select * from privilegios where usr_id='" & id & "'", dataset)

            For c As Integer = 0 To dataset.Tables("data").Rows.Count - 1
                Select Case dataset.Tables("data").Rows(c)(2)
                    Case "usuarios"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            usuarios.Checked = True
                        End If
                    Case "bodegas"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            bodegas.Checked = True
                        End If
                    Case "estudios"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            estudios.Checked = True
                        End If
                    Case "movimientos"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            movimientos.Checked = True
                        End If
                    Case "kits"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            kits.Checked = True
                        End If
                    Case "investigadores"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            investigadores.Checked = True
                        End If
                    Case "inspectores"
                        If (dataset.Tables("data").Rows(c)(3) = "1") Then
                            inspectores.Checked = True
                        End If


                End Select
            Next
        Else
            Button3.Hide()
        End If
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        If ValidarRut(TextBox1.Text, TextBox1, ErrorProvider1) = False Or ValidaExistente(TextBox1.Text, TextBox1, ErrorProvider1) = False Then
            TextBox1.Focus()
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'MsgBox("valor= " & ComboBox1.Display & "indice=" & ComboBox1.SelectedValue)
    End Sub


    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim qry As String

        If ValidarVacio(Me, Me.ErrorProvider1) = True Then
            qry = "update usuarios set usr_rut='" & TextBox1.Text & "',usr_nombre='" & TextBox2.Text & "',usr_apellido='" & TextBox3.Text & "',usr_clave='" & TextBox4.Text & "',grup_id='" & ComboBox1.SelectedValue & "',usr_email='" & TextBox7.Text & "',usr_telefono='" & TextBox5.Text & "',usr_tele_fijo='" & TextBox6.Text & "' where usr_id='" & id & "'"
            EjecutaConsulta(qry)
            For Each control In Me.GroupBox1.Controls
                If TypeOf control Is CheckBox Then
                    EjecutaConsulta("update privilegios set pri_permiso='" & control.CheckState & "' where usr_id='" & id & "' and pri_seccion='" & control.Name & "' ")
                End If
            Next
            Me.Close()
            Try
                EjecutaConsulta(qry)
            Catch ex As SqlClient.SqlException
                MsgBox("Error: " & ex.Message)
            End Try
            'registro movimiento
            registro("modificar", login.usuario.id, main.modulo.usuario)
        End If
        man_usuarios.actualizar()
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox4_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox5_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox6_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

    Private Sub TextBox7_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub

End Class